using System;
using System.Threading.Tasks;
using JlPay.Sdk.Core;
using JlPay.Sdk.Services.Trans.Mini.Wx;
using JlPay.Sdk.Models.Trans.Mini.Wx;

namespace JlPay.Sdk.Examples.Trans.Mini.Wx
{
    /// <summary>
    /// 交易 - Mini微信公众号/小程序支付功能使用示例
    /// </summary>
    public class OfficialPayDemo
    {
        /// <summary>
        /// 构建微信公众号/小程序支付请求
        /// </summary>
        /// <returns>请求对象</returns>
        private static OfficialPayRequest BuildOfficialPayRequest()
        {
            var request = new OfficialPayRequest();
            
            // 设置请求参数
            request.MchId = "849584358120018";
            request.TermNo = "60131234";
            request.PayType = "wxpay";
            request.OpenId = "oUpF8uMuAJO_M2pxb1Q9zNjWeS6o";
            request.SubAppid = "wxd930ea53421a258f4f";
            request.LimitPay = "no_credit";
            request.OutTradeNo = "ld201911023465529";
            request.PaymentValidTime = "10";
            request.Body = "测试微信公众号支付商品";
            request.Attach = "测试微信公众号支付描述";
            request.TotalFee = "150";
            request.Remark = "测试微信公众号支付备注";
            request.NotifyUrl = "https://help.jlpay.com/api/docs/home";
            request.OpUserId = "test_user";
            request.OpShopId = "GD_SZ_001";
            request.DeviceInfo = "N50007A8890";
            request.MchCreateIp = "127.0.0.1";
            request.Longitude = "116.397128";
            request.Latitude = "39.916527";
            request.GoodsTag = "test_tag";
            
            return request;
        }

        /// <summary>
        /// 微信公众号/小程序支付示例
        /// </summary>
        public static async Task OfficialPayDemoAsync()
        {
            Console.WriteLine("开始执行微信公众号/小程序支付示例");

            // 使用DemoConfig创建配置对象
            var config = DemoConfig.CreateConfigWithCrypto();
            Console.WriteLine("配置初始化完成，已启用自动加密和解密功能");

            var officialPayService = new OfficialPayService(config);

            // 创建微信公众号/小程序支付请求
            var request = BuildOfficialPayRequest();

            try
            {
                // 发送请求
                var response = await officialPayService.OfficialPayAsync(request);

                // 使用优化的响应处理
                response.HandleResponse("微信公众号/小程序支付", config);
            }
            catch (Exception ex)
            {
                // 使用优化的异常处理
                var response = new OfficialPayResponse();
                response.HandleResponseWithException("微信公众号/小程序支付", config, ex);
            }

            Console.WriteLine("微信公众号/小程序支付示例执行完毕");
        }

        /// <summary>
        /// 主程序入口
        /// </summary>
        public static async Task Main(string[] args)
        {
            Console.WriteLine("=" + new string('=', 50));
            Console.WriteLine("===== 微信公众号/小程序支付示例 开始 =====");
            Console.WriteLine("=" + new string('=', 50));

            await OfficialPayDemoAsync();

            Console.WriteLine("=" + new string('=', 50));
            Console.WriteLine("===== 微信公众号/小程序支付示例执行完毕 =====");
            Console.WriteLine("=" + new string('=', 50));
        }
    }
} 